package battle
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;

	public class AnimateObject extends BaseObject
	{
		//每帧宽度
		public var frameWidth:int;
		
		//每帧高度
		public var frameHeight:int;
		
		//总列数
		public var cols:int;
		
		//总行数
		public var rows:int;
		
		//动画总帧数
		public var frames:int;
		
		//每秒播放的帧数
		public var fps:int;
		
		//重复播放次数,如果小于等于0,则表示无限循环
		public var totalRepeatTimes:int
		
		//当前播放帧数
		public var curFrame:int;
		
		//播放过的次数
		public var repeatedTimes:int;
		
		
		
		public function AnimateObject()
		{
			super();
		}
		
		public function setupAnimate( bitmapData:BitmapData, rect:Rectangle, fps:int, totalRepeatTimes:int ):void
		{	
			frameWidth =  rect.width;
			frameHeight = rect.height;
			
			cols = bitmapData.width / frameWidth;
			rows = bitmapData.height / frameHeight;
			
			frames = cols * rows; 
			
			_graphic = new Graphic( bitmapData, null );
			
			this.totalRepeatTimes = totalRepeatTimes;
			
			this.fps = fps;
			
			repeatedTimes = 0;
			
			curFrame = 0;
			
		}
		
		public override function onEnterFrame(dt:int):void
		{
			if( frames > 1 )
			{
				var frameOffset:int = int( Number( dt ) / 1000 * fps );
				
				curFrame = ( curFrame + frameOffset ) % frames;
				
				if( totalRepeatTimes > 0 )
				{
					if( curFrame == 0 && frameOffset > 0 )
					{
						repeatedTimes++;	
					}
					
					if( totalRepeatTimes == repeatedTimes )
					{
						destroy();
					}
				}
					
			}
			
		}
		
		public override function getGraphic():Graphic
		{
			var rect:Rectangle = new Rectangle( curFrame / rows % cols * frameWidth, curFrame / rows * frameHeight, frameWidth, frameHeight );
			
			this._graphic.rect = rect;
			
			return _graphic;
		}
		
		
	}
}